package com.example.client.zkClient;

import com.example.client.ZKConstans;
import org.I0Itec.zkclient.IZkDataListener;
import org.I0Itec.zkclient.ZkClient;

/**
 * @author Creared by wangyang on 2019-01-28 18:14
 * email:techyang.wangy.@ymm56.com
 */
public class GetDataSample {
    public static void main(String[] args) throws InterruptedException {
        final String path = "/zk-book";

        ZkClient zkClient = new ZkClient(ZKConstans.CONNECTOR_STRING, 5000);
        zkClient.createEphemeral(path, "123");
        zkClient.subscribeDataChanges(path, new IZkDataListener() {
            @Override
            public void handleDataChange(String dataPath, Object data) throws Exception {
                System.out.println("Node " + dataPath + " changed. new data: " + data);
            }

            @Override
            public void handleDataDeleted(String dataPath) throws Exception {
                System.out.println("Node " + path + " deleted...");

            }
        });

        System.out.println((String) zkClient.readData(path));

        zkClient.writeData(path, "456");
        Thread.sleep(1000);
        zkClient.delete(path);
        Thread.sleep(Integer.MAX_VALUE);
    }
}
